package com.ky.study.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ky.common.param.ProblemAdd;
import com.ky.common.vo.R;
import com.ky.study.entity.Static;
import com.ky.study.mapper.StaticMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;

/**
 * <p>
 * 静态资源表 前端控制器
 * </p>
 *
 * @author wgz
 * @since 2024-06-17
 */
@RestController
@RequestMapping("/study/static")
public class StaticController {
    @Autowired
    StaticMapper staticMapper;
    @RequestMapping("/articleImg/{articleId}")
    public R articleImg(@PathVariable int articleId){
        QueryWrapper<Static> staticQueryWrapper = new QueryWrapper<>();
        staticQueryWrapper.eq("article_id",articleId);
        return R.ok(staticMapper.selectList(staticQueryWrapper));
    }

    @RequestMapping("/addProblemImg")
    public R addProblemImg(HttpServletRequest request, @RequestBody ProblemAdd problemAdd){

        try {
            // 处理上传的图片
            for (String image : problemAdd.getUrlList()) {
                Static staticResource = new Static();
                staticResource.setProblemId(problemAdd.getProblemId());
                staticResource.setFileUrl(image);
                staticResource.setCreatedAt(new Date());
                staticMapper.insert(staticResource);
            }
        } catch (Exception e) {
            e.printStackTrace();
            return R.fail("上传失败");
        }
        return R.ok();
    }


}

